#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int generateFrame(char* oneFrame, int nWidth, int nHeight, int nRandom)
{
	char* y = oneFrame;
	char* u = oneFrame + nWidth * nHeight;
	char* v = u + nWidth * nHeight / 4;

	/*RGB to YUV*/
	int Red = 0xe1;
	int Green = 0x69;
	int Blue = 0xce;

	int Y = 0.299 * Red + 0.587 * Green + 0.114 * Blue;
	int U = -0.1687 * Red - 0.3313 * Green + 0.5 * Blue + 128;
	int V = 0.5 * Red - 0.4187 * Green - 0.0813 * Blue + 128;

	memset(y, Y, nWidth * nHeight);
	memset(u, U, nWidth * nHeight / 4);
	memset(v, V, nWidth * nHeight / 4);
	return nWidth * nHeight * 3 / 2;
}

int maintest()
{
	int nWidth = 640;
	int nHeight = 480;
	int nFrameNum = 2;
	const char* filename = "test.yuv";
	int i = 0;
	FILE* fp = NULL;
	char* onePic = NULL;
	int nLen = 0;

	onePic = (char*)malloc(nWidth * nHeight * 3 / 2);
	if (onePic == NULL)
	{
		printf("malloc err\n");
		return -1;
	}
	memset(onePic, 0, sizeof(onePic));
	fp = fopen(filename, "wb+");
	if (fp == NULL)
	{
		printf("open file %s err\n", filename);
		free(onePic);
		return -1;
	}

	for (; i < nFrameNum; i++)
	{
		nLen = generateFrame(onePic, nWidth, nHeight, i);
		fwrite(onePic, nLen, 1, fp);
	}
	free(onePic);
	fclose(fp);
	return 0;
}
